//基本类型
let six:number = 6;
let hexLiteral:number = 0xf00d;
let names:string = "bob";
let sentence:string = `Hello, my name is ${ name }.`
let u: undefined = undefined;
let n: null = null;

//元祖  Tuple
const messyArray = ['something',2,true,undefined,null];
const tuple: [number,string,string] = [24 , 'Indrek', 'Lasn']
const messyArrays : [string,number,boolean,undefined,null] = ['something',2,true,undefined,null];

//枚举 enum
enum Color { Red = 1 , Green = 2 , Blue = 4}
let c: Color = Color.Green;
let colorName:string = Color[2];
console.log(c)
console.log(colorName)

//函数中定义返回类型 Void   若没有返回值就会报错
function sayMyName(name:string): string {
	return name;
}

console.log(sayMyName("indrek"))

function sayMyNames(name:string): void {
	console.log(name)
}
sayMyNames("陈一飞")

//Any   什么类型都行
let person: any = "前端劝退师"
person = 25
person = true

//Never
// 返回never的函数必须存在无法达到的终点
function error(message: string): never {
    throwError(message);
}

// 推断的返回值类型为never
function fail() {
    return error("Something failed");
}

// 返回never的函数必须存在无法达到的终点
function infiniteLoop(): never {
    while (true) {
    }
}


//类型断言
let someValue: any = "thing is a string"
let strLength: number = (<string>someValue).length;
let strLengths: number = (someValue as string).length;  

//function getLength(something: string | number ) : number {
//	return something.length;
//}

function getLengths(something:string | number){
	if((<string>something).length){
		return (something as string).length;
	}else{
		return something.toString().length;
	}
}
console.log(getLengths(someValue))

//泛型  Generics
function gen_func1<T>(arg: T): T {
    return arg;
}
// 或者
let gen_func2: <T>(arg: T) => T = function (arg) {
    return arg;
}


gen_func1<string>('Hello world');
gen_func2('Hello world'); 


//泛型与Any
// 方法一：带有any参数的方法
function any_func(arg: any): any {
    console.log(arg.length);
		return arg;
}

// 方法二：Array泛型方法
function array_func<T>(arg: Array<T>): Array<T> {
	  console.log(arg.length);
		return arg;
}


//自定义类型：Interface vs Type alias
interface User { 
	name:string
	age:number
}

type Users = {
	name:string
	age:number
}

interface SetUser {
	(name: string , age:number):void
}

type NameResolver = () => void;

//都允许拓展（extends)
interface Name {
	name:string;
}

interface User extends Name {
	age: number
}

//type Names = { 
//  name: string; 
//}

//type Users = Name & { age: number  };



